## creates tables for appendix G:
## plots for analysis of over time trend
## figures A8(a-b)

rm(list=ls())

library(tidyverse)


# read in data
op_list <- readRDS('bootstrap_gaps_data/opinion_bootstrapped_allquestions.RDS')

for (i in names(op_list)) {
  assign(paste0(i, '_allqs'), op_list[[i]])
}

# do usual transformations
dem_polarization_boot_allqs <- abs(dem_top_boot_allqs - dem_bottom_boot_allqs)
rep_polarization_boot_allqs <- abs(rep_top_boot_allqs - rep_bottom_boot_allqs)
all_polarization_boot_allqs <- abs(all_top_boot_allqs - all_bottom_boot_allqs)


pol_allqs <- expand.grid(year = sort(unique(issuetopics_allqs$year)),
                         party = c('rep', 'dem', 'all'),
                         polarization = NA,
                         hici = NA,
                         loci = NA,
                         stringsAsFactors = FALSE)

for (i in 1:nrow(pol_allqs)) {
  tmp.ref <- issuetopics_allqs$qid[issuetopics_allqs$year == pol_allqs$year[i]]
  
  tmp.boot <- get(paste0(pol_allqs$party[i], '_polarization_boot_allqs'))[tmp.ref,]
  
  tmp.dist <- apply(tmp.boot, MARGIN = 2, mean)
  
  pol_allqs$polarization[i] <- median(tmp.dist)
  pol_allqs$hici[i] <- quantile(sort(tmp.dist), 0.975)
  pol_allqs$loci[i] <- quantile(sort(tmp.dist), 0.025)
}



# figure A8(a) - all repeat issues

ggplot(pol_allqs,
       aes(x = as.integer(year))) +
  geom_point(aes(y = polarization, color = party), size = 2) +
  geom_line(aes(y = polarization, color = party), size = 0.75) +
  geom_errorbar(aes(ymin = loci, ymax = hici, color = party), size = 0.5, width = 0.25) +
  theme_bw() +
  scale_color_manual(values = c('gray40', 'blue', 'red'),
                     breaks = c('all', 'dem', 'rep'),
                     labels = c('All Respondents', 'Democrats', 'Republicans')) +
  scale_x_continuous(breaks = c(2010, 2012, 2014, 2016, 2018, 2020)) +
  labs(x = 'Year',
       y = 'Class Opinion Gap',
       title = '',
       color = '') +
  theme(legend.position = 'bottom')
ggsave('appendix_figures/overtime_allqs.png', height = 4, width = 5.5)


# figure A8(b) - repeat issues by topic

pol_bytopic_allqs <- expand.grid(year = sort(unique(issuetopics_allqs$year)),
                                 party = c('rep', 'dem', 'all'),
                                 topic_6 = sort(unique(issuetopics_allqs$topic_6)),
                                 polarization = NA,
                                 hici = NA,
                                 loci = NA,
                                 stringsAsFactors = FALSE)

for (i in 1:nrow(pol_bytopic_allqs)) {
  tmp.ref <- issuetopics_allqs$qid[issuetopics_allqs$year == pol_bytopic_allqs$year[i] & 
                                     issuetopics_allqs$topic_6 == pol_bytopic_allqs$topic_6[i]]
  
  tmp.boot <- as.matrix(get(paste0(pol_bytopic_allqs$party[i], '_polarization_boot_allqs'))[tmp.ref,])
  
  tmp.dist <- apply(tmp.boot, MARGIN = 2, mean)
  
  pol_bytopic_allqs$polarization[i] <- median(tmp.dist)
  pol_bytopic_allqs$hici[i] <- quantile(sort(tmp.dist), 0.975)
  pol_bytopic_allqs$loci[i] <- quantile(sort(tmp.dist), 0.025)
}

domains.labs <- c('Culutral' ,'Economic', 'Foreign Policy', 'Immigration', 'Law Enforcement', 'Social Welfare')
names(domains.labs) <- c('cultural', 'economic', 'foreignpolicy', 'immigration', 'lawenforcement', 'socialwelfare')

ggplot(pol_bytopic_allqs,
       aes(x = as.integer(year))) +
  geom_point(aes(y = polarization, color = party)) +
  geom_line(aes(y = polarization, color = party)) +
  geom_errorbar(aes(ymin = loci, ymax = hici, color = party), size = 0.5, width = 0.25) +
  facet_wrap(vars(topic_6), labeller = as_labeller(domains.labs)) +
  theme_bw() +
  scale_color_manual(values = c('gray40', 'blue', 'red'),
                     breaks = c('all', 'dem', 'rep'),
                     labels = c('All Respondents', 'Democrats', 'Republicans')) +
  scale_x_continuous(breaks = c(2010, 2012, 2014, 2016, 2018, 2020)) +
  labs(x = 'Year',
       y = 'Class Opinion Gap',
       title = '',
       color = '') +
  theme(legend.position = 'bottom')
ggsave('appendix_figures/overtime_allqs_bytopic.png', height = 4, width = 6)
